package com.qf.config;

import com.alibaba.fastjson2.JSON;
import com.qf.util.R;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Serializable;
import java.util.HashMap;

//无认证异常处理
@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {

        response.setStatus(200);
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        String msg="请求访问"+request.getRequestURL()+"访问失败";
        //转化为JSON
        String r= JSON.toJSONString(R.fail(40003,msg));
        //响应出去
        response.getWriter().println(r);
        //需要在配置类加入使用
    }
}
